<?php
// ==============================================================================================
// Licensed under the GNU GPLv3 (LICENSE.txt)
// ==============================================================================================
// @author     WEBO Software (http://www.webogroup.com/)
// @version    0.1.0
// @copyright  Copyright &copy; 2011 WEBO Software, All Rights Reserved
// ==============================================================================================
// no direct access
defined('_JEXEC') or die('Restricted access');

function com_install() {
    $adminDir = dirname(__FILE__);
?><table class="adminheading" cellpadding="4" cellspacing="0" border="0"><tr><td><?php
	$msgstart = '<h2 style="color:red">There was a problem with your installation</h2>';
	$msgend = '<h2>Please, read <a target="_blank" href="http://code.google.com/p/webo-video-transmuter/wiki/TroubleshootingAndSupport">TroubleShooter</a> or <a target="_blank" href="http://code.google.com/p/webo-video-transmuter/issues/list">Known Issues</a><br/>Thanks!</h2>';
/* check if path for system plugin is writable */
    if (!@is_writable(JPATH_SITE . "/plugins/system/")) {
        echo $msgstart . '<p>The directory <code>' .
			JPATH_SITE .
			'/plugins/system/</code> is not writeable</p><h2><p>Please:</p><ul><li>Uninstall WEBO Video Transmuter component.</li><li>Modify permissions for the directory <code>' .
			JPATH_SITE .
			'/plugins/system/</code> to 0755 or 0775 or 0777.</li><li>Reinstall WEBO Video Transmuter.</li></ul><br/>Thanks!</p></h2>';
        return false;
    }
/* check for php version */
	if (phpversion() < 5) {
		echo $msgstart . '<p>PHP 5+ is required!</p>' . $msgend;
		return false;
	}

/* copy system plugin files */
	$install_log = "Installation of WEBO Video Transmuter plugin: ";
	@copy($adminDir . '/videotransmuter.php', JPATH_SITE . '/plugins/system/videotransmuter.php');
	@copy($adminDir . '/videotransmuter.x', JPATH_SITE . '/plugins/system/videotransmuter.xml');

    if (!@is_file(JPATH_SITE . '/plugins/system/videotransmuter.xml')) {
    	echo $msgstart . '<p style="color:red"><b>Error when creating WEBO Video Transmuter plugin. Please check if directory ' . JPATH_SITE . '/plugins/system/ is writable.</p>' . $msgend; 
		return false;
    }

/* publish system plugin */
	$database = & JFactory::getDBO();
    $database->setQuery(  "INSERT INTO `#__plugins` "
    					. "        (`name`, `element`, `folder`, `access`, `ordering`, `published`, `iscore`, `client_id`, `checked_out`, `checked_out_time`, `params`) "
    					. " VALUES ('System - WEBO Video Transmuter', 'videotransmuter', 'system', 0, 0, 1, 0, 0, 0, '0000-00-00 00:00:00', '');");
	$result = $database->query();

	if (!$result) {
    	echo $msgstart . '<p>Plugins install error: ' . $database->stderr() . '</p>' . $msgend;
    	return false;
	}

	$user =& JFactory::getUser();
	$user_email = $user->get('email');
	require(JPATH_SITE . DS . 'components' . DS . 'com_webovideotransmuter' . DS . 'core' . DS . 'controller' . DS . 'admin.php');
	$webo_video_transmuter_admin = new webo_video_transmuter_admin();
	$webo_video_transmuter_admin->install(JPATH_SITE);

/* check if cache directory is writable */	
	if (!@is_writable($webo_video_transmuter_admin->config->options['videodir'])) {
		echo $msgstart . '<p>Can\'t write to the directory (please chmod it to 0775): ' . $webo_video_transmuter_admin->config->options['videodir'] . '</p>' . $msgend;
		return false;
	}

?></td></tr></table><?php

/* include language file */
	$language = strtolower(preg_replace("/[-,;].*/", "",
		empty($_SERVER["HTTP_ACCEPT_LANGUAGE"]) ? 'en' : $_SERVER["HTTP_ACCEPT_LANGUAGE"]));
	if (!empty($_COOKIE['wss_lang'])) {
		$language = strtolower($_COOKIE['wss_lang']);
	}
	$language = preg_replace("/[^a-z]/", "", $language);
	if (@is_file(JPATH_SITE . DS . 'components' . DS . 'com_webovideotransmuter' . DS . 'core' . DS . 'libs' . DS . 'locale' . DS . $language . '.php')) {
		require(JPATH_SITE . DS . 'components' . DS . 'com_webovideotransmuter' . DS . 'core' . DS . 'libs' . DS . 'locale' . DS . $language . '.php');
	} else {
		require(JPATH_SITE . DS . 'components' . DS . 'com_webovideotransmuter' . DS . 'core' . DS . 'libs' . DS . 'locale' . DS . 'en.php');
	}
	echo _WEBO_ABOUT_TEXT;
}
?>